﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SamrtToolApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dilog = new FolderBrowserDialog();
            dilog.Description = "请选择文件夹";
            if (dilog.ShowDialog() == DialogResult.OK || dilog.ShowDialog() == DialogResult.Yes)
            {
                txtPath.Text = dilog.SelectedPath;
            }
        }

        private void btnGen_Click(object sender, EventArgs e)
        {
            string path = txtPath.Text;
            string ext = txtExt.Text;
            string[] extArray = ext.Split('|');
            List<FileInfo> lst = new List<FileInfo>();
            //遍历文件夹
            List<FileInfo> lstFiles = getFile(path, ext, lst);
            int lineNum = 0;
            int.TryParse(txtLineNum.Text, out lineNum);
            //写入文件
            writeFile(lstFiles, lineNum);
        }

        private void writeFile(List<FileInfo> lstFiles,int lineCount)
        {
            int lineNum = 0;
            string fileName = "合并" + System.DateTime.Now.ToString("yyyMMddHHmmss") + ".txt";
            using (StreamWriter sw = new StreamWriter(fileName))
            {
                foreach (FileInfo shpFile in lstFiles)
                {
                    try
                    {
                        // 创建一个 StreamReader 的实例来读取文件 
                        // using 语句也能关闭 StreamReader
                        using (StreamReader sr = new StreamReader(shpFile.FullName))
                        {
                            this.lbLog.Items.Insert(0, "read file:" + shpFile.Name + " start...");
                            string line;
                            // 从文件读取并显示行，直到文件的末尾 
                            while ((line = sr.ReadLine()) != null)
                            {
                                sw.WriteLine(line);
                                if (++lineNum > lineCount)
                                {
                                    break;
                                }
                            }
                            this.lbLog.Items.Insert(0, "read file:" + shpFile.Name + " end!");
                        }
                    }
                    catch (Exception e)
                    {
                        // 向用户显示出错消息
                        this.lbLog.Items.Insert(0,"The file could not be read:");
                        this.lbLog.Items.Insert(0,e.Message);
                    }
                    if (lineNum > lineCount)
                    {
                        break;
                    }
                }
            }
            this.lbLog.Items.Insert(0, "生成完毕，共"+ lineNum + "行！");
        }

        /// <summary>
            /// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹)
            /// </summary>
            /// <param name="path">文件夹路径</param>
            /// <param name="extName">扩展名可以多个 例如 .mp3.wma.rm</param>
            /// <returns>List<FileInfo></returns>
        public static List<FileInfo> getFile(string path, string extName, List<FileInfo> lst)
        {
            try
            {
                string[] dir = Directory.GetDirectories(path); //文件夹列表  
                DirectoryInfo fdir = new DirectoryInfo(path);
                FileInfo[] file = fdir.GetFiles();
                //FileInfo[] file = Directory.GetFiles(path); //文件列表  
                if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空          
                {
                    foreach (FileInfo f in file) //显示当前目录所有文件  
                    {
                        string ext = f.Extension.ToLower();
                        if ((!string.IsNullOrEmpty(ext)) && extName.ToLower().IndexOf(ext) >= 0)
                        {
                            lst.Add(f);
                        }
                    }
                    foreach (string d in dir)
                    {
                        getFile(d, extName, lst);//递归  
                    }
                }
                return lst;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
